* LTV missing 
foreach x of varlist orig_loan_to_value origcombltv debt_to_income {
	replace `x' = . if inlist(`x', 999, -999)
}

foreach x of varlist orig_rate netnoterate orig_loan_amount orig_term credit_score num_units num_borrowers {
	replace `x' = . if `x' == -999
}

* dummy vars
gen CorrespondentOrig = tpo_flag == "C"
gen BrokerOrig = tpo_flag == "B"
gen PurchaseLoan = purpose == "P"
gen OwnerOccupied = inlist(occ_status, "O", "P")

gen PropType_CO = proptype_id == "CO"
gen PropType_PU = proptype_id == "PU"
gen PropType_others = proptype_id ~= "SF" &  PropType_CO ==0 & PropType_PU==0


** Conforming loan limit
gen cll = 417000
replace cll = 424100 if inrange(orig_ym, ym(2017, 1), ym(2017, 12))
replace cll = 453100 if inrange(orig_ym, ym(2018, 1), ym(2018, 12))

gen JumboConforming = (orig_loan_amount > cll) * (numunit == 1)


gen MonRate = orig_rate / 1200
gen PandI = orig_loan_amount* MonRate* (1+ MonRate)^orig_term/ ((1+ MonRate)^orig_term - 1)
gen income = 12* PandI/ debt_to_income* 100
gen mtoi = orig_loan_amount/ income
drop MonRate PandI
